package com.wanli.storemobile.mine;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wanli.storemobile.R;
import com.wanli.storemobile.base.BaseActivity;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.UserInfoBean;
import com.wanli.storemobile.bean.WechatStateBean;
import com.wanli.storemobile.mine.model.MineModelImpl;
import com.wanli.storemobile.utils.Base64BitmapUtil;
import com.wanli.storemobile.utils.GlideUtils;
import com.wanli.storemobile.utils.SPManager;
import com.wanli.storemobile.utils.SaveImgTools;
import com.wanli.storemobile.utils.ToastUtil;
import com.wanli.storemobile.widget.AppTitleBar;
import com.wanli.storemobile.widget.ViewLoading;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WeiXinAuthActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.iv_photo)
    ImageView ivPhoto;
    @BindView(R.id.tv_merchant_no)
    TextView tvMerchantNo;
    @BindView(R.id.tv_tishi)
    TextView tvTishi;
    @BindView(R.id.iv_dianyin_photo)
    ImageView ivDianyinPhoto;
    private MineModelImpl mineModel = new MineModelImpl();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_xin_auth);
        ButterKnife.bind(this);
        initData();
    }

    public void initData() {

        mineModel.requestStoreWechatState(new DataCallBack<WechatStateBean>() {
            @Override
            public void onSuccessful(WechatStateBean bean) {
                tvTishi.setVisibility(View.VISIBLE);
                String qrurl = bean.getData().getQrurl();
                GlideUtils.loadImage(WeiXinAuthActivity.this,ivPhoto,qrurl);

//                tvMerchantNo.setText("商户号：" + bean.getData().getWechatMerchantNo());
                ivPhoto.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {

                        SaveImgTools.SaveImageToSysAlbum(WeiXinAuthActivity.this, ivPhoto,qrurl);


//                        saveScreenShot(viewConversionBitmap(ivPhoto));
                        return true;
                    }
                });
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
//
//        ViewLoading.showProgress(mActivity);
//        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
//
//
//
//        if (userInfoBean != null && userInfoBean.getData() != null) {
//            if (userInfoBean.getData().getQuick_income() == 2) {
//                ivDianyinPhoto.setVisibility(View.VISIBLE);
//                ivPhoto.setVisibility(View.GONE);
//                tvMerchantNo.setVisibility(View.GONE);
//                mineModel.requestStoreWechatStateDianYin(new DataCallBack<WechatStateBean>() {
//                    @Override
//                    public void onSuccessful(WechatStateBean bean) {
//                        tvTishi.setVisibility(View.VISIBLE);
//
//
//
//                        GlideUtils.downloadBitmap(mActivity, ivDianyinPhoto, bean.getData().getQrurl(), new GlideUtils.BitmapLoadListener() {
//                            @Override
//                            public void getBitmap(Bitmap bitmap) {
//
//                            }
//                        });
//
//                    }
//
//                    @Override
//                    public void onFailed(String msg, String code) {
//
//                    }
//                });
//            } else {
//                ivDianyinPhoto.setVisibility(View.GONE);
//                ivPhoto.setVisibility(View.VISIBLE);
//                tvMerchantNo.setVisibility(View.VISIBLE);
//                mineModel.requestStoreWechatState(new DataCallBack<WechatStateBean>() {
//                    @Override
//                    public void onSuccessful(WechatStateBean bean) {
//                        tvTishi.setVisibility(View.VISIBLE);
//                        String qrurl = bean.getData().getQrurl();
//
//                        GlideUtils.loadImage(WeiXinAuthActivity.this,ivPhoto,qrurl);
//
//                        tvMerchantNo.setText("商户号：" + bean.getData().getWechatMerchantNo());
//                        ivPhoto.setOnLongClickListener(new View.OnLongClickListener() {
//                            @Override
//                            public boolean onLongClick(View v) {
//                                saveScreenShot(viewConversionBitmap(ivPhoto));
//                                return true;
//                            }
//                        });
//                    }
//
//                    @Override
//                    public void onFailed(String msg, String code) {
//
//                    }
//                });
//            }
//        }
    }
    /**
     * view转bitmap
     */
    public Bitmap viewConversionBitmap(View v) {
        int w = v.getWidth();
        int h = v.getHeight();
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        c.drawColor(Color.WHITE);
        /** 如果不设置canvas画布为白色，则生成透明 */
        v.layout(0, 0, w, h);
        v.draw(c);
        return bmp;
    }
    /**
     * 屏幕截图保存
     */
    private void saveScreenShot(Bitmap bitmap) {
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        OutputStream outStream = null;
        String filename;//声明文件名
        //以保存时间为文件名
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        filename = sdf.format(date);
        File file = new File(extStorageDirectory, filename + ".JPEG");//创建文件，第一个参数为路径，第二个参数为文件名
        try {
            outStream = new FileOutputStream(file);//创建输入流
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
            outStream.close();

            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            mActivity.sendBroadcast(intent);
            ToastUtil.showShort("保存相册成功");
//            Vibrator vibrator = (Vibrator) mActivity.getSystemService(mActivity.VIBRATOR_SERVICE);
//            vibrator.vibrate(200);
        } catch (Exception e) {
            ToastUtil.showShort("保存相册失败，可尝试系统截屏保存");
        }
    }

}
